package com.uenpay.im.ui.old.activity.chat;

import android.graphics.Bitmap;
import android.os.AsyncTask;

import com.qiyunxin.android.http.common.ImageUtil;
import com.qiyunxin.android.http.utils.FileUtils;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.entities.chatbean.AttachModel;
import com.uenpay.im.entities.chatbean.MsgImageModel;

/**
 * @Author: SL
 * @Date: 2017/4/12 14:11
 * @CopyRight: http://www.qiyunxin.com
 * @Parameter: TODO
 * @Function: TODO
 */
public class SendImageAsyncTask extends AsyncTask<Void, Void, Void> {

    interface ISendImageATListener{
        void onResult(MsgImageModel imageModel);
    }
    private Bitmap compressBitmap = null;
    private byte[] bytes = null;
    private String big_pic_filename;
    private String picPath;
    private FileUtils fileUtils;
    private ISendImageATListener mISendImageATListener;

    public SendImageAsyncTask(String _picPath, ISendImageATListener _ISendImageATListener) {
        mISendImageATListener = _ISendImageATListener;
        picPath = _picPath;
        fileUtils = new FileUtils(IMApplication.appName);
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        big_pic_filename = fileUtils.getAlbumDir() + "/"
                + Utils.md5(picPath) + ".jpg";

        compressBitmap = ImageUtil.CompressImageSameRadioScale(picPath,
                100, 960, 960);
        bytes = fileUtils.compressPicAndSaveReturnBytes(compressBitmap,
                picPath, Bitmap.CompressFormat.JPEG, 90);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (bytes == null) {
            return;
        } else {
            MsgImageModel imageModel = new MsgImageModel();
            AttachModel attachmentModel = new AttachModel();
            attachmentModel.setFileLocalPath(picPath);
            attachmentModel.setPic_height(compressBitmap.getHeight() + "");
            attachmentModel.setPic_width(compressBitmap.getWidth() + "");
            imageModel.setAttachmentModel(attachmentModel);
            mISendImageATListener.onResult(imageModel);

        }

        if (compressBitmap != null && !compressBitmap.isRecycled()) {
            compressBitmap.recycle();
        }
    }

}
